package com.gqj.bas.service.impl;

import com.gqj.bas.domain.BasDevice;
import com.gqj.bas.mapper.BasDeviceMapper;
import com.gqj.bas.service.IBasDeviceService;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备Service业务层处理
 * 
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class BasDeviceServiceImpl implements IBasDeviceService 
{
    @Autowired
    private BasDeviceMapper basDeviceMapper;

    /**
     * 查询设备
     * 
     * @param deviceId 设备主键
     * @return 设备
     */
    @Override
    public BasDevice selectBasDeviceByDeviceId(Long deviceId)
    {
        return basDeviceMapper.selectBasDeviceByDeviceId(deviceId);
    }

    /**
     * 查询设备列表
     * 
     * @param basDevice 设备
     * @return 设备
     */
    @Override
    public List<BasDevice> selectBasDeviceList(BasDevice basDevice)
    {
        return basDeviceMapper.selectBasDeviceList(basDevice);
    }

    /**
     * 新增设备
     * 
     * @param basDevice 设备
     * @return 结果
     */
    @Override
    public int insertBasDevice(BasDevice basDevice)
    {
        basDevice.setCreateTime(DateUtils.getNowDate());
        return basDeviceMapper.insertBasDevice(basDevice);
    }

    /**
     * 修改设备
     * 
     * @param basDevice 设备
     * @return 结果
     */
    @Override
    public int updateBasDevice(BasDevice basDevice)
    {
        basDevice.setUpdateTime(DateUtils.getNowDate());
        return basDeviceMapper.updateBasDevice(basDevice);
    }

    /**
     * 批量删除设备
     * 
     * @param deviceIds 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteBasDeviceByDeviceIds(Long[] deviceIds)
    {
        return basDeviceMapper.deleteBasDeviceByDeviceIds(deviceIds);
    }

    /**
     * 删除设备信息
     * 
     * @param deviceId 设备主键
     * @return 结果
     */
    @Override
    public int deleteBasDeviceByDeviceId(Long deviceId)
    {
        return basDeviceMapper.deleteBasDeviceByDeviceId(deviceId);
    }

    /**
     * 校验设备信息是否唯一
     *
     * @param basDevice 设备信息
     * @return 结果
     */
    @Override
    public String checkDeviceCodeUnique(BasDevice basDevice) {
        Long deviceId = StringUtils.isNull(basDevice.getDeviceId()) ? -1L : basDevice.getDeviceId();
        BasDevice info = basDeviceMapper.checkDeviceCodeUnique(basDevice.getDeviceCode());
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验设备信息是否唯一
     *
     * @param basDevice 设备信息
     * @return 结果
     */
    @Override
    public String checkDeviceIpUnique(BasDevice basDevice) {
        Long deviceId = StringUtils.isNull(basDevice.getDeviceId()) ? -1L : basDevice.getDeviceId();
        BasDevice info = basDeviceMapper.checkDeviceIpUnique(basDevice.getIp());
        if (StringUtils.isNotNull(info) && info.getDeviceId().longValue() != deviceId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
